#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "bridge.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_view = new QWebEngineView(this);
    setCentralWidget(m_view);
    m_channel = new QWebChannel(this);          //创建通道对象用于和JS交互
    bridge *myBridge = new bridge;              //创建通道对象用于和JS交互
    m_channel->registerObject("mybridge", (QObject*)myBridge);// 这里注册对象名"mybridge"需要与JS函数用到的名称一致
    m_view->page()->setWebChannel(m_channel);

    QString htmlPath = QApplication::applicationDirPath() + "/index.html";
    m_view->page()->load("file:///" + htmlPath);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionPie_triggered()
{
    QString sHtml =
            "\
            option={\
            title:{\
            text:'饼图程序调用高亮示例',\
            left:'center'\
            },\
            tooltip:{\
            trigger:'item',\
            formatter:'{a}<br/>{b}:{c}({d}%)'\
            },\
            legend:{\
            orient:'vertical',\
            left:'left',\
            data:['直接访问','邮件营销','联盟广告','视频广告','搜索引擎']\
            },\
            series:[\
            {\
            name:'访问来源',\
            type:'pie',\
            radius:'55%',\
            center:['50%','60%'],\
            data:[\
            {value:335,name:'直接访问'},\
            {value:310,name:'邮件营销'},\
            {value:234,name:'联盟广告'},\
            {value:135,name:'视频广告'},\
            {value:1548,name:'搜索引擎'}\
            ],\
            emphasis:{\
            itemStyle:{\
            shadowBlur:10,\
            shadowOffsetX:0,\
            shadowColor:'rgba(0,0,0,0.5)'\
            }\
            }\
            }\
            ]\
            }\
            ";

    m_view->page()->runJavaScript(QString("showPie(%1)").arg(sHtml));
}

